/******************************************************************************
 * Copyright (C) 2024 EternalChip, Inc.(Gmbh) or its affiliates.
 *
 * All Rights Reserved.
 *
 * @file
 *
 * @par dependencies
 * -
 * -
 * -
 *
 * @author
 *
 * @brief
 *
 * Processing flow:
 *
 * call directly.
 *
 * @version
 *
 *****************************************************************************/
#include "gpio.h"

#include "key.h"

uint8_t        Get_KeyStatus(STRUCT_IO_SLIDING_FILTER *pKey_Sliding_Filter);
static void    Update_SildingFilter(STRUCT_IO_SLIDING_FILTER *pIO_Sliding_Filter,
                                    uint16_t                  u16New_Value);
static uint8_t Check_KeyStatus(void);

/*********************************************************************************************************
 *                                      Get_KeyStatus()
 *
 * @brief
 *
 * @param   void
 *
 * @retval  void
 *
 * @note    None.
 ********************************************************************************************************/
uint8_t Get_KeyStatus(STRUCT_IO_SLIDING_FILTER *pKey_Sliding_Filter)
{
    Update_SildingFilter(pKey_Sliding_Filter, Check_KeyStatus());

    // 按键状态为未按下，但检测到有效值大于8次，认为按键被按下
    if (pKey_Sliding_Filter->u8State == 0 && pKey_Sliding_Filter->u16Valid_Value > 8)
    {
        pKey_Sliding_Filter->u8State = 1;

        return 1;
    }

    // 按键状态为按下，但检测到无效值大于8次，认为按键被释放
    if (pKey_Sliding_Filter->u8State == 1 && pKey_Sliding_Filter->u16Invalid_Value > 8)
    {
        pKey_Sliding_Filter->u8State = 0;

        return 0;
    }
		
		return 0;
}

/*********************************************************************************************************
 *                           Update_SildingFilter()
 *
 * @brief
 *
 * @param   void
 *
 * @retval  void
 *
 * @note    None.
 ********************************************************************************************************/
void Update_SildingFilter(STRUCT_IO_SLIDING_FILTER *pIO_Sliding_Filter, uint16_t u16New_Value)
{
    if (pIO_Sliding_Filter->u16Count == pIO_Sliding_Filter->u16Size)
    {
        pIO_Sliding_Filter->u16Valid_Value -=
            pIO_Sliding_Filter->u16Buf[pIO_Sliding_Filter->u16Head];
    }
    else
    {
        pIO_Sliding_Filter->u16Count++;
    }

    pIO_Sliding_Filter->u16Buf[pIO_Sliding_Filter->u16Head] = u16New_Value;

    // Update Head
    pIO_Sliding_Filter->u16Head = (pIO_Sliding_Filter->u16Head + 1) % pIO_Sliding_Filter->u16Size;

    pIO_Sliding_Filter->u16Valid_Value += u16New_Value;
    pIO_Sliding_Filter->u16Invalid_Value =
        pIO_Sliding_Filter->u16Size - pIO_Sliding_Filter->u16Valid_Value;
}

/*********************************************************************************************************
 *                                Check_KeyStatus()
 *
 * @brief   Check Key pin status.
 *
 * @param   void
 *
 * @retval  1: The key is pressed.
 *          0: The Key is released.
 *
 * @note    None.
 ********************************************************************************************************/
uint8_t Check_KeyStatus(void)
{
    return (HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_RESET ? 1 : 0);
}
